using UnityEngine;

[DisallowMultipleComponent]
public class WeaponShootEffect : MonoBehaviour
{
    private ParticleSystem shootEffectParticleSystem;

    private void Awake()
    {
        // 加载组件
        shootEffectParticleSystem = GetComponent<ParticleSystem>();
    }

    /// <summary>
    /// 从传入的 WeaponShootEffectSO 和 aimAngle 中设置射击效果
    /// </summary>
    public void SetShootEffect(WeaponShootEffectSO shootEffect, float aimAngle)
    {
        // 设置拍摄效果颜色渐变
        SetShootEffectColorGradient(shootEffect.colorGradient);

        // 设置射击效果粒子系统起始值
        SetShootEffectParticleStartingValues(shootEffect.duration, shootEffect.startParticleSize,
            shootEffect.startParticleSpeed, shootEffect.startLifetime, shootEffect.effectGravity,
            shootEffect.maxParticleNumber);

        // 设置射击效果粒子系统粒子爆发粒子数
        SetShootEffectParticleEmission(shootEffect.emissionRate, shootEffect.burstParticleNumber);

        // 设置发射器旋转
        SetEmmitterRotation(aimAngle);

        // 设置射击效果粒子精灵
        SetShootEffectParticleSprite(shootEffect.sprite);

        // 设置拍摄效果、生存期、最小和最大速度
        SetShootEffectVelocityOverLifeTime(shootEffect.velocityOverLifetimeMin, shootEffect.velocityOverLifetimeMax);
    }

    /// <summary>
    /// 设置拍摄效果、粒子系统、颜色渐变
    /// </summary>
    private void SetShootEffectColorGradient(Gradient gradient)
    {
        // 设置颜色渐变
        ParticleSystem.ColorOverLifetimeModule colorOverLifetimeModule = shootEffectParticleSystem.colorOverLifetime;
        colorOverLifetimeModule.color = gradient;
    }

    /// <summary>
    /// 设置射击效果粒子系统起始值
    /// </summary>
    private void SetShootEffectParticleStartingValues(float duration, float startParticleSize, float startParticleSpeed,
        float startLifetime, float effectGravity, int maxParticles)
    {
        ParticleSystem.MainModule mainModule = shootEffectParticleSystem.main;

        // 设置粒子系统持续时间
        mainModule.duration = duration;

        // 设置粒子起始大小
        mainModule.startSize = startParticleSize;

        // 设置粒子启动速度
        mainModule.startSpeed = startParticleSpeed;

        // 设置粒子启动生存期
        mainModule.startLifetime = startLifetime;

        // 设置粒子起始重力
        mainModule.gravityModifier = effectGravity;

        // 设置最大粒子数
        mainModule.maxParticles = maxParticles;
    }

    /// <summary>
    /// 设置射击效果粒子系统粒子爆发粒子数
    /// </summary>
    private void SetShootEffectParticleEmission(int emissionRate, float burstParticleNumber)
    {
        ParticleSystem.EmissionModule emissionModule = shootEffectParticleSystem.emission;

        // 设置粒子爆发次数
        ParticleSystem.Burst burst = new ParticleSystem.Burst(0f, burstParticleNumber);
        emissionModule.SetBurst(0, burst);

        // 设置粒子发射率
        emissionModule.rateOverTime = emissionRate;
    }

    /// <summary>
    /// 设置射击效果粒子系统精灵
    /// </summary>
    private void SetShootEffectParticleSprite(Sprite sprite)
    {
        // 设置粒子爆发次数
        ParticleSystem.TextureSheetAnimationModule textureSheetAnimationModule =
            shootEffectParticleSystem.textureSheetAnimation;

        textureSheetAnimationModule.SetSprite(0, sprite);
    }

    /// <summary>
    /// 设置发射器的旋转以匹配瞄准角度
    /// </summary>
    private void SetEmmitterRotation(float aimAngle)
    {
        transform.eulerAngles = new Vector3(0f, 0f, aimAngle);
    }

    /// <summary>
    /// 设置生命周期内的拍摄效果速度
    /// </summary>
    private void SetShootEffectVelocityOverLifeTime(Vector3 minVelocity, Vector3 maxVelocity)
    {
        ParticleSystem.VelocityOverLifetimeModule velocityOverLifetimeModule =
            shootEffectParticleSystem.velocityOverLifetime;

        // 定义最小最大 X 速度
        ParticleSystem.MinMaxCurve minMaxCurveX = new ParticleSystem.MinMaxCurve();
        minMaxCurveX.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveX.constantMin = minVelocity.x;
        minMaxCurveX.constantMax = maxVelocity.x;
        velocityOverLifetimeModule.x = minMaxCurveX;

        // 定义最小最大 Y 速度
        ParticleSystem.MinMaxCurve minMaxCurveY = new ParticleSystem.MinMaxCurve();
        minMaxCurveY.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveY.constantMin = minVelocity.y;
        minMaxCurveY.constantMax = maxVelocity.y;
        velocityOverLifetimeModule.y = minMaxCurveY;

        // 定义最小最大 Z 速度
        ParticleSystem.MinMaxCurve minMaxCurveZ = new ParticleSystem.MinMaxCurve();
        minMaxCurveZ.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveZ.constantMin = minVelocity.z;
        minMaxCurveZ.constantMax = maxVelocity.z;
        velocityOverLifetimeModule.z = minMaxCurveZ;
    }
}